package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Dbtable;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtilsWx;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/9
 * @Time: 下午3:03
 */
public class CopyController extends BaseController {


    @Override
    public void list() {
        String title = getPara("title");
        String type = "dbtable";
        String sql = "from dbtable where 1=1 ";
        if (!StringUtilsWx.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }

        Page<Dbtable> data = Dbtable.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("dbtable_list.html");
    }


    public void copy() {
        String title = getPara("title");
        String type = "dbtable";
        String sql = "from dbtable where 1=1 ";
        if (!StringUtilsWx.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
        Page<Dbtable> data = Dbtable.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("dbtable_list_dialog.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Dbtable img = Dbtable.dao.findById(id);
        setAttr("dbtable", img);
        render("dbtable_edit.html");
    }

    @Override
    public void save() {
        UploadFile uploadFile = getFile("cover");
        Dbtable us = getModel(Dbtable.class, "dbtable");
        if (us != null) {
            if (us != null && us.getId() != null) {
                us.update();
            } else if(us!=null&&us.getId()==null) {
                us.save();
            }
        }
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from dbtable where id in (" + ids + ")");
        list();
    }


}
